package dbBean;

import java.sql.*;
import java.util.*;


public class DBConnManager {
	
	private Vector poolnames = new Vector();
	
	private Vector drivernames = new Vector();
	
	private Vector dbids = new Vector();
	
	private Vector usernames = new Vector();
	
	private Vector passwds = new Vector();
	
	private Vector maxconns = new Vector();
	
	private Hashtable connPools = new Hashtable();
	
	public DBConnManager() {
		
		poolnames.addElement("mysql");
		drivernames.addElement("org.gjt.mm.mysql.Driver");
		dbids.addElement("jdbc:mysql://localhost/ksxt?useUnicode=true&characterEncoding=GBK");
		usernames.addElement("root");
		passwds.addElement("xyg");
		maxconns.addElement("300");
		
		
		poolnames.addElement("access");
		drivernames.addElement("sun.jdbc.odbc.JdbcOdbcDriver");
		dbids.addElement("jdbc:odbc:ksxtData");
		usernames.addElement("");
		passwds.addElement("");
		maxconns.addElement("300");
		
		
		createPools();
	}
	
	
	public void releaseConnection(String name, Connection con) {
		DBConnPool pool = (DBConnPool) connPools.get(name);
		if (pool != null)
			pool.releaseConnection(con);
	}
	
	
	public Connection getConnection(String name) {
		DBConnPool pool = (DBConnPool) connPools.get(name);
		if (pool != null)
			return pool.getConnection();
		return null;
	}
	
	
	public synchronized void closeConns() {
		Enumeration allPools = connPools.elements();
		while (allPools.hasMoreElements()) {
			DBConnPool pool = (DBConnPool) allPools.nextElement();
			pool.closeConn();
		}
	}
	
	
	private void createPools() {
		for(int i = 0; i<poolnames.size();i++){
			String poolname = poolnames.elementAt(i).toString();
			String drivername = drivernames.elementAt(i).toString();
			String dbid = dbids.elementAt(i).toString();
			String username = usernames.elementAt(i).toString();
			String passwd = passwds.elementAt(i).toString();
			int maxconn=0;
			try {
				maxconn = Integer.parseInt(maxconns.elementAt(i).toString());
			}
			catch (NumberFormatException e) {
				e.printStackTrace();
			}
			DBConnPool pool = new DBConnPool(poolname, drivername, dbid, username, passwd, maxconn);
			connPools.put(poolname, pool);
		}
	}
	
	public static void main(String[] args) throws SQLException {
		DBConnManager dbConnManager = new DBConnManager();
		Connection conn=dbConnManager.getConnection("mysql");
		if(conn==null)
		{
			System.out.println("<h2 align='center'>对不起，现在数据库忙，请稍后再试！</h2>");
			System.out.println("<h2 align='center'><a href='index.jsp'>返回</a></h2>");
		} else {
			System.out.println("success");
			
			Statement stmt=conn.createStatement();
			ResultSet rs;
			rs=stmt.executeQuery("select * from kszlb where xh='200734652037' and mm='0899'");
			if(rs.next())
			{
				//System.out.println(rs.getInt(1));
				System.out.println("姓名: " + rs.getString("xm"));  //姓名
			}
			rs.close();
			
			dbConnManager.releaseConnection("mysql",conn);
			dbConnManager.closeConns();
		}
		
	}
}

